package com.example.iwalk.newprintapplication.imp;

import android.util.Log;

import com.example.iwalk.newprintapplication.constant.SocketState;
import com.example.iwalk.newprintapplication.inter.SocketInter;
import com.example.iwalk.newprintapplication.inter.SocketStateChangeListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 网络连接
 * Created by iwalk on 2016/12/16.
 */

public class NetSocket implements SocketInter {
    private Socket socket;
    private static final String DEBUG_TAG = "NetSocket";
    public static final int POS_OPEN_NETPORT = 9100;// 0X238c
    public boolean IFOpen = false;
    public int Net_ReceiveTimeout = 2500;
    private SocketStateChangeListener stateChangeListener;

    public NetSocket( String host, int port,SocketStateChangeListener stateChangeListener) {
        this.stateChangeListener =stateChangeListener;
        try {
            this.stateChangeListener.onStateChanged(SocketState.STATE_1);
            if ("".equals(host)) {
                this.stateChangeListener.onConnectFailed(SocketState.ERROR_0);
            }
            socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
            this.stateChangeListener.onConnectFailed(SocketState.ERROR_1);
//            Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
        }
    }

    //网络打印机 打开网络打印机
    public boolean Open(String ipaddress, int netport) {
        if (socket == null) {
            try {
                SocketAddress ipe = new InetSocketAddress(Inet4Address.getByName(ipaddress), netport);
//                Log.e(DEBUG_TAG, Inet4Address.getByName(ipaddress)+"|"+netport);
                socket = new Socket();  //Socket(ipaddress, netport,true);
                socket.connect(ipe, Net_ReceiveTimeout);
                IFOpen = true;
            } catch (Exception e) {
                IFOpen = false;
            }
        } else {
            try {
                socket.close();
                SocketAddress ipe = new InetSocketAddress(Inet4Address.getByName(ipaddress), netport);
                Log.e(DEBUG_TAG, Inet4Address.getByName(ipaddress) + "|" + netport);
                socket = new Socket();  //Socket(ipaddress, netport,true);
                socket.connect(ipe, Net_ReceiveTimeout);
                IFOpen = true;
            } catch (Exception e) {
                IFOpen = false;

            }
        }
        if (socket == null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return IFOpen;
    }

    //网络打印机 关闭
    @Override
    public void close() {
        try {
            stateChangeListener.onStateChanged(SocketState.STATE_4);
            socket.close();
            socket = null;
            stateChangeListener.onStateChanged(SocketState.STATE_41);
        } catch (Exception e) {
            e.printStackTrace();
            stateChangeListener.onConnectFailed(SocketState.ERROR_4);
        }
    }

    public NetSocket() {

    }

    @Override
    public OutputStream getSocketOut() {
        OutputStream socketOut = null;
        try {
            stateChangeListener.onStateChanged(SocketState.STATE_2);
            socketOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            stateChangeListener.onConnectFailed(SocketState.ERROR_2);
        }
        return socketOut;
    }

    @Override
    public InputStream getSocketIn() {
        InputStream socketIn = null;
        try {
            stateChangeListener.onStateChanged(SocketState.STATE_5);
            socketIn = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            stateChangeListener.onConnectFailed(SocketState.ERROR_5);
        }
        return socketIn;
    }

    //获取本机IPv4
    public static String getLocalHostIp() {
        try {
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            // 遍历所用的网络接口
            while (en.hasMoreElements()) {
                NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
                Enumeration<InetAddress> inet = nif.getInetAddresses();
                // 遍历每一个接口绑定的所有ip
                while (inet.hasMoreElements()) {
                    InetAddress ip = inet.nextElement();
                    if (!ip.isLoopbackAddress() && ip instanceof Inet4Address) {
                        return ip.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            Log.e("sdj", "获取本地ip地址失败");
            e.printStackTrace();
        }
        return null;
    }

}
